package com.ams.service;

import com.ams.entity.*;
import com.ams.mapper.CommonMapper;
import com.ams.mapper.MemberMapper;
import com.ams.mapper.OrderMapper;
import com.ams.util.ListUtil;
import org.apache.tomcat.jni.Mmap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author liuxiaoding
 * @data 2019/7/5
 */
@Service
public class CommonService {

    @Autowired
    private CommonMapper commonMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private  MemberService memberService;


    public void js_cp(Member member, Integer mid,BigDecimal pv_total){

        String ids = member.getLastUserIds();
        if (ids==null||ids.length()==1) return;
        ids = ids.substring(1,ids.length()-1);

        String[] str = ids.split(",");
        List<String> list= new ArrayList<>(Arrays.asList(str));
        Collections.reverse(list);
        for(int i=0;i<list.size();i++){
            Integer pid=Integer.valueOf(list.get(i));
            System.out.println("第"+(i+1)+"层");
            if(i!=23)continue;
            Integer cnum=i+1;
            List<CpRecord> cpRecords=commonMapper.getCpByPidAndNum(pid,cnum);
            if(ListUtil.isNotEmpty(cpRecords)) continue;

            QuLog quLog=commonMapper.getQuLogByPidAndCid(pid,mid);

            if(null==quLog) continue;

            //取对面区最先报单的最小值
            Integer qu_type=1;//左区
            if(quLog.getType()==1){
                qu_type=2;
            }
            Order order=orderMapper.getZcD(pid,qu_type,quLog.getCount());
            if(null==order) continue;

            //对碰类
            Member dmember=memberMapper.getMemberByOpenId(order.getOpenid());
            BigDecimal credit1_bl= new BigDecimal(0.5);
            BigDecimal credit2_bl= new BigDecimal(0.5);

            if(i==0) {//第一层
                credit1_bl= new BigDecimal(0.25);
                credit2_bl= new BigDecimal(0.25);
            }
            if(pv_total.compareTo(order.getPvTotal())!=0){
                pv_total=new BigDecimal(600);
            }
            //车房积分
            BigDecimal credit1_num = credit1_bl.multiply(pv_total);
            if(credit1_num.compareTo(new BigDecimal(0))==1){
                memberService.setCredit(pid,2,credit1_num,"层奖日结第"+quLog.getCount()+"层 来自"+member.getUserBn()+"和"+dmember.getUserBn());
            }

            //封顶 现金积分
            BigDecimal credit2_num = pv_total;
            credit2_num = credit2_bl.multiply(credit2_num);
            if(credit2_num.compareTo(new BigDecimal(0))==1){
                memberService.setCredit(pid,1,credit2_num,"层奖日结第"+quLog.getCount()+"层 来自"+member.getUserBn()+"和"+dmember.getUserBn());
            }

            CpRecord cpRecord=new CpRecord(pid,mid,dmember.getId(),i+1,1,mid);

            commonMapper.addCpRecord(cpRecord);

        }
        System.out.println(123);

    }

}
